﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Reflection;
using System.Data;

namespace Diagram.DynamicDiagram
{
    public class DataModel
    {
        #region 构造函数
        public DataModel(String filepath, DataSet ds)
        {
            initializeData(filepath, ds);
        }
        #endregion

        #region 属性
        private Data _DEPTMEAS;                         // 存储井深数据
        private List<Data> _dataList;                   // 存储数据
        private DataSet _dataSet;                       // 数据源

        private double _defaultScaleWidth;              // Scale列默认宽度
        private double _defaultColumnHeight;            // 默认列高
        private double _minColumnWidth;                 // 默认列最小宽度
        private double _defaultHeaderHeight;            // 默认表头高度
        private double _minHeaderHeight;                // 最小表头高度
        private double _defaultColumnNumber;            // 默认显示列数量

        private double _scale;                          // 刻度

        public Data DEPTMEAS
        {
            get { return _DEPTMEAS; }
            set { _DEPTMEAS = value; }
        }

        public List<Data> DataList
        {
            get { return _dataList; }
            set { _dataList = value; }
        }

        public DataSet DataSet
        {
            get { return _dataSet; }
            set { _dataSet = value; }
        }

        public double DefaultScaleWidth
        {
            get { return _defaultScaleWidth; }
            set { _defaultScaleWidth = value; }
        }

        public double DefaultColumnHeight
        {
            get { return _defaultColumnHeight; }
            set { _defaultColumnHeight = value; }
        }

        public double MinColumnWidth
        {
            get { return _minColumnWidth; }
            set { _minColumnWidth = value; }
        }

        public double DefaultHeaderHeight
        {
            get { return _defaultHeaderHeight; }
            set { _defaultHeaderHeight = value; }
        }

        public double MinHeaderHeight
        {
            get { return _minHeaderHeight; }
            set { _minHeaderHeight = value; }
        }

        public double DefaultColumnNumber
        {
            get { return _defaultColumnNumber; }
            set { _defaultColumnNumber = value; }
        }

        public double Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }
        #endregion

        #region 私有函数
        private void initializeData(string filepath, DataSet ds)
        {
            // 读取默认配置
            XmlDocument doc = new XmlDocument();
            doc.Load("..\\..\\DynamicDiagram\\DiagramConfig.xml");
            XmlNode node = doc.SelectSingleNode("Diagram/ColumnScale/width");
            _defaultScaleWidth = Int32.Parse(node.InnerText);
            node = doc.SelectSingleNode("Diagram/Column/defaultCanvasHeight");
            _defaultColumnHeight = Int32.Parse(node.InnerText);
            node = doc.SelectSingleNode("Diagram/Column/minWidth");
            _minColumnWidth = Int32.Parse(node.InnerText);
            node = doc.SelectSingleNode("Diagram/HeaderHeight/minHeight");
            _defaultHeaderHeight = Int32.Parse(node.InnerText);
            node = doc.SelectSingleNode("Diagram/HeaderHeight/defaultHeight");
            _minHeaderHeight = Int32.Parse(node.InnerText);

            // 读取数据
            doc.Load(filepath);
            XmlNode root = doc.DocumentElement;
            XmlNodeList list = root.ChildNodes;

            _DEPTMEAS = new DEPTMEAS();
            _DEPTMEAS.initializeData(ds);
            
            // 计算刻度
            _scale = (_DEPTMEAS.Max - _DEPTMEAS.Min) / _defaultColumnHeight;

            // 读取剩余数据
            _dataList = new List<Data>();
            node = list[0];
            XmlNodeList dataList = node.ChildNodes;
            foreach (XmlNode xn in dataList)
            {
                Assembly assembly = Assembly.GetAssembly(Type.GetType(xn.InnerText));
                Data d = (Data)assembly.CreateInstance(xn.InnerText);
                d.initializeData(ds);
                _dataList.Add(d);
            }

            node = list[1];
            dataList = node.ChildNodes;
            _defaultColumnNumber = dataList.Count;
            for (int i = 0; i < _defaultColumnNumber; ++i)
            {
                XmlNode xn = dataList[i];
                String[] str = xn.InnerText.Split(',');
                for (int loop = 0; loop < str.Length; ++loop)
                {
                    String[] nameArray = str[loop].Trim().Split('.');
                    String name = nameArray[2].Trim();
                    foreach (Data d in _dataList)
                    {
                        if (d.Name == name)
                        {
                            d.DefaultColumnPos.Add(loop + 1);
                            break;
                        }
                    }
                }
            }
        }
        #endregion

        #region 公有函数
        public void appendData()
        { 
            
        }
        #endregion
    }
}
